//
//  MPGLBaseViewController.m
//  mingpianguanli
//
//  Created by ryou on 2021/6/23.
//

#import "MPGLBaseViewController.h"

@interface MPGLBaseViewController ()<BUNativeExpressBannerViewDelegate>
@property(nonatomic,strong)BUNativeExpressBannerView *minngPianBannerView;
@end

@implementation MPGLBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=kMpglColor;
    
    // Do any additional setup after loading the view.
}
-(NSMutableArray *)mpglArray{
    if (!_mpglArray) {
        _mpglArray=[[NSMutableArray alloc]init];
    }
    return _mpglArray;
}

- (void)mingpianguanliloadBannerWithSlotID{
    [self.minngPianBannerView removeFromSuperview];
    
    UIWindow *window = nil;
    if ([[UIApplication sharedApplication].delegate respondsToSelector:@selector(window)]) {
        window = [[UIApplication sharedApplication].delegate window];
    }
    if (![window isKindOfClass:[UIView class]]) {
        window = [UIApplication sharedApplication].keyWindow;
    }
    if (!window) {
        window = [[UIApplication sharedApplication].windows objectAtIndex:0];
    }
    CGFloat bottom = 0.0;
    if (@available(iOS 11.0, *)) {
        bottom = window.safeAreaInsets.bottom;
    } else {
        // Fallback on earlier versions
    }
    

    CGSize size = CGSizeMake(600, 90);
    //    native_banner_ID
    NSString *realSlotId = kbannerkey;
    // important: 升级的用户请注意，初始化方法去掉了imgSize参数

    self.minngPianBannerView = [[BUNativeExpressBannerView alloc] initWithSlotID:realSlotId rootViewController:self adSize:size interval:30];
    self.minngPianBannerView.layer.masksToBounds = YES;
   
    self.minngPianBannerView.frame = CGRectMake(0, kmpglHeight-size.height-bottom, size.width, size.height);
    
    // 不支持中途更改代理，中途更改代理会导致接收不到广告相关回调，如若存在中途更改代理场景，需自行处理相关逻辑，确保广告相关回调正常执行。
    self.minngPianBannerView.delegate = self;
    [self.minngPianBannerView loadAdData];

}

- (void)showBanner {
    [self.view addSubview:self.minngPianBannerView];
}

#pragma BUNativeExpressBannerViewDelegate
- (void)nativeExpressBannerAdViewDidLoad:(BUNativeExpressBannerView *)bannerAdView {
    [self pbud_logWithSEL:_cmd msg:@""];
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView didLoadFailWithError:(NSError *)error {
    [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
}

- (void)nativeExpressBannerAdViewRenderSuccess:(BUNativeExpressBannerView *)bannerAdView {
    [self pbud_logWithSEL:_cmd msg:@""];
    [self showBanner];
}

- (void)nativeExpressBannerAdViewRenderFail:(BUNativeExpressBannerView *)bannerAdView error:(NSError *)error {
    [self pbud_logWithSEL:_cmd msg:[NSString stringWithFormat:@"error:%@", error]];
}

- (void)nativeExpressBannerAdViewWillBecomVisible:(BUNativeExpressBannerView *)bannerAdView {
    [self pbud_logWithSEL:_cmd msg:@""];
}

- (void)nativeExpressBannerAdViewDidClick:(BUNativeExpressBannerView *)bannerAdView {
    [self pbud_logWithSEL:_cmd msg:@""];
}

- (void)nativeExpressBannerAdView:(BUNativeExpressBannerView *)bannerAdView dislikeWithReason:(NSArray<BUDislikeWords *> *)filterwords {
    [self pbud_logWithSEL:_cmd msg:@""];
}

- (void)nativeExpressBannerAdViewDidCloseOtherController:(BUNativeExpressBannerView *)bannerAdView interactionType:(BUInteractionType)interactionType {
    NSString *str;
    if (interactionType == BUInteractionTypePage) {
        str = @"ladingpage";
    } else if (interactionType == BUInteractionTypeVideoAdDetail) {
        str = @"videoDetail";
    } else {
        str = @"appstoreInApp";
    }
    [self pbud_logWithSEL:_cmd msg:str];
}

- (void)nativeExpressBannerAdViewDidRemoved:(BUNativeExpressBannerView *)nativeExpressAdView {

    [UIView animateWithDuration:0.25 animations:^{
        nativeExpressAdView.alpha = 0;
    } completion:^(BOOL finished) {
        [nativeExpressAdView removeFromSuperview];
        self.minngPianBannerView = nil;
    }];
}

- (void)pbud_logWithSEL:(SEL)sel msg:(NSString *)msg {
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
